package pipe.bytestream;

import java.io.*;

class Sender implements Runnable {

    private PipedOutputStream out = new PipedOutputStream();

    private File srcFile;

    public Sender(File srcFile) {
        this.srcFile = srcFile;
    }

    public PipedOutputStream getOutputStream() {
        return out;
    }

    public void run() {
        BufferedInputStream bin = null;
        try {

            bin = new BufferedInputStream(new FileInputStream(this.srcFile));
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = bin.read(buffer)) != -1) {
                //System.out.println(s);
                out.write(buffer, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bin != null) {
                    bin.close();
                    bin = null;
                }
                if (out != null) {
                    out.close();
                    out = null;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

class Receiver implements Runnable {
    private PipedInputStream in = new PipedInputStream();

    private File destFile;

    public Receiver(File destFile) {
        this.destFile = destFile;
    }

    public PipedInputStream getInputStream() {
        return in;
    }

    public void run() {
        BufferedOutputStream bout = null;
        byte[] b = new byte[1024];
        try {
            bout = new BufferedOutputStream(new FileOutputStream(destFile));
            String s = null;
            int len = -1;
            while ((len = in.read(b)) != -1) {
                bout.write(b, 0, len);
            }
            bout.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bout != null) {
                    bout.close();
                    bout = null;
                }
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

public class PipedOutputStreamDemo {

    public static void main(String[] args) {
        File srcFile = new File("d:" + File.separator + "MyUtils.java");
        File destFile = new File("e:" + File.separator + "MyUtils.java");
        try {
            Sender sender = new Sender(srcFile);    // 创建线程对象Sender
            Receiver receiver = new Receiver(destFile);    // 创建线程对象Receiver
            PipedOutputStream out = sender.getOutputStream();    // 写入
            PipedInputStream in = receiver.getInputStream();    // 读出
            out.connect(in); // 将输出发送到输入
            Thread senderThread = new Thread(sender);
            Thread receiverThread = new Thread(receiver);
            senderThread.start();// 启动线程
            receiverThread.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
